from random import randint
from urllib.parse import quote

from django.core.cache import cache
from django.http import JsonResponse


def get_state():
    res = ""
    for i in range(8):
        res += str(randint(0, 9))
    return res


def apply_code(request):
    appid = "624"
    redirect_uri = quote("https://game.syh118.top/settings/acwing/acapp/receive_code/")
    scope = "userinfo"
    state = get_state()

    cache.set(state, True, 7200)  # 有效期两小时

    return JsonResponse({
        "result": "success",
        "appid": appid,
        "redirect_uri": redirect_uri,
        "scope": scope,
        "state": state,
    })
